{% extends "layout.html" %}
{% block content %}
    <div class="x-body layui-anim layui-anim-up">
        <form class="layui-form" lay-filter="form">
			<table class="layui-table">
				<tbody>
                    <!-- <tr>
						<th><span class="x-red">*</span>通道名称</th>
						<td colspan="1">
                            <input type="text" id="name" name="name" required="required" lay-verify="required"
                            autocomplete="off" class="layui-input">
                        </td>
						<th><span class="x-red">*</span>类型</th>
						<td colspan="1">
                            <input type="radio" id="type" name="type" lay-filter="type" value="0" title="公用" checked>
                            <input type="radio" id="type" name="type" lay-filter="type" value="1" title="单通道">
                            <input type="radio" id="type" name="type" lay-filter="type" value="2" title="多通道">
                        </td>
                    </tr> -->
					<tr>
						<th>地址</th>
						<td>
                            <input type="text" id="address" name="address" required="required|number" lay-verify="required"
                            autocomplete="off" class="layui-input" value="0">
						</td>
						<th>数量</th>
						<td>
                            <input type="text" id="data_count" name="data_count" required="required|number" lay-verify="required"
                            autocomplete="off" class="layui-input" value="1">
						</td>
					</tr>
					<tr>
						<th>起始值</th>
						<td>
                            <input type="text" id="data_start" name="data_start" required="required|number" lay-verify="required"
                            autocomplete="off" class="layui-input" value="0">
						</td>
						<th>结束值</th>
						<td>
                            <input type="text" id="data_end" name="data_end" required="required|number" lay-verify="required"
                            autocomplete="off" class="layui-input" value="10">
						</td>
					</tr>
					<tr>
						<th>数据类型</th>
						<td>
							<select id="data_type" name="data_type" lay-filter="data_type">
                                <option value="1">整型</option>
                                <option value="2">浮点型</option>
                            </select>
						</td>
						<th>生成方法</th>
						<td>
							<select id="method" name="method" lay-filter="method">
                                <option value="1">递增</option>
                                <option value="2">随机</option>
                                <!-- <option value="3">列表值</option> -->
                            </select>
						</td>
					</tr>
						<th>生成周期</th>
						<td colspan="3">
                            <input type="text" id="run_cycle" name="run_cycle" required="required|number" lay-verify="required"
                            autocomplete="off" class="layui-input" value="1000">
						</td>
					<tr>
						<td colspan="2" style="text-align: center;">
                            <button id="confirmBtn" class="layui-btn" lay-filter="save" lay-submit="">
                                增加
                            </button>
                        </td>
						<td colspan="2" style="text-align: center;">
							<button  type="reset" class="layui-btn">
                                重置
                            </button>
						</td>
					</tr>
                </tbody>
            </table>
        </form>
    </div>
    <script>
        var data_simulator_id=GetQueryString('data_simulator_id');
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
            var form = layui.form
            ,layer = layui.layer;
            if(data_simulator_id!=null){
                data={'data_simulator_id':data_simulator_id};
                $('#confirmBtn').html('保存');
                $.ajax({
                    url:'readDataSimulator'
                    ,type:'POST'
                    ,dataType:'json'
                    ,data:data
                    ,success:function(data){
                        //设置内容
                        if(data.code==0){
                            data_simulator_id=data.data[0].data_simulator_id;
                            form.val("form",{
                                address:data.data[0].address,
                                data_count:data.data[0].data_count,
                                data_start:data.data[0].data_start,
                                data_end:data.data[0].data_end,
                                data_type:data.data[0].data_type,
                                method:data.data[0].method,
                            });
                        }
                    }
                });
            }
            //自定义验证规则
            // form.verify({
            //     host_ip: function(value){
            //         if(!["1","3","5"].includes($("#address").val()))return;
            //         var exp=/^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))).){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$/;
            //         if(!exp.test(value)){
            //             return '必须是合法的IPV4地址';
            //         }
            //     },
            // });
            //监听提交
            form.on('submit(save)', function(data){
                if(data_simulator_id!=null)
                    data.field['data_simulator_id']=data_simulator_id;
                $.ajax({url:'saveDataSimulator',type:'POST',dataType:'json',data:data.field,success:function(data){
                    if(data.code==0){
                        // layer.msg('操作成功!');
                        layer.alert("操作成功", {icon: 6},function () {
                            parent.location.reload();
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    }
                }});
                return false;
            });
        });
    </script>
{% endblock %}